import { createStore, createLogger, GetterTree } from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import { IgetDetailByNoticeSerialNo } from '@/typings/getDetailByNoticeSerialNo';
import { commonTypes } from '@/typings/commonTypes';
import { IqueryDictionaries } from '@/typings/queryDictionaries';
import { IqueryBindPatInfo } from '@/typings/queryBindPatInfo';

const debug = process.env.NODE_ENV !== 'production'; //在开发环境打开日志
interface IpayloadItem {
  keyOne: string;
  keyTwo?: string | null;
  valueAndObj: commonTypes.ICodeName | any;
}

interface State {
  isConfirm: string;
  form: IgetDetailByNoticeSerialNo.BusObj;
  dictionaries: IqueryDictionaries.IBusObj; //字典
  provCityCounty: commonTypes.ISieDictionaries[]; //接口的省市区字典
  hospitalId: string | number;
  hospitalUUID: string | number;
  provCityCountyColumns: commonTypes.ISieDictionaries[]; //组装后的省市区
  currentUser: IqueryBindPatInfo.IBusObj; //选择的当前用户
}
/*interface IGetters {
  form: (state: State) => IgetDetailByNoticeSerialNo.BusObj;
  getIsConfirm: (state: State) => any;
  getFormDocAdmiNote: (state: State) => IgetDetailByNoticeSerialNo.DocAdmiNote;
  getFormNurseAdmiNote: (state: State) => IgetDetailByNoticeSerialNo.NurseAdmiNote;
  getFormPatAdmiNote: (state: State) => IgetDetailByNoticeSerialNo.PatAdmiNote;
  getDictionaries: (state: State) => IqueryDictionaries.IBusObj;
}*/
export default createStore<State>({
  plugins: [createPersistedState()], //debug ? [createPersistedState(), createLogger()] : [createPersistedState()],
  strict: process.env.NODE_ENV !== 'production',
  state: {
    isConfirm: '', //0:取消，1-确认,2-待确认，3-未安排床位
    form: <IgetDetailByNoticeSerialNo.BusObj>{
      docAdmiNote: {},
      nurseAdmiNote: {},
      patAdmiNote: {},
    },
    dictionaries: {} as IqueryDictionaries.IBusObj, //字典的存储
    provCityCounty: [], //省市区字典
    hospitalId: 4, //医院id
    hospitalUUID: 'prod_gxykdey_uuid_00000181024370',
    provCityCountyColumns: [],
    currentUser: {} as IqueryBindPatInfo.IBusObj,
  },
  getters: {
    form: (state) => state.form,
    getIsConfirm: (state) => state.isConfirm,
    getFormDocAdmiNote: (state) => state.form.docAdmiNote,
    getFormNurseAdmiNote: (state) => state.form.nurseAdmiNote,
    getFormPatAdmiNote: (state) => state.form.patAdmiNote,
    getDictionaries: (state) => state.dictionaries,
    getProvCityCounty: (state) => state.provCityCounty,
    getProvCityCountyColumns: (state) => state.provCityCountyColumns,
  },
  mutations: {
    setForm(state, payload) {
      state.form = { ...state.form, ...payload };
    },
    setIsConfirm(state, payload) {
      state.isConfirm = payload;
    },
    setProvCityCounty(state, payload) {
      state.provCityCounty = payload;
    },
    //指定修改某一个属性值
    setFormItem(state, payload: IpayloadItem) {
      // eslint-disable-next-line no-debugger
      //debugger;
      //console.log(1111, state.form[payload.keyOne]);
      payload.keyOne && payload.keyTwo
        ? (state.form[payload.keyOne][payload.keyTwo] = payload.valueAndObj)
        : (state.form[payload.keyOne] = payload.valueAndObj);
    },
    setFormDocAdmiNote(state, payload) {
      state.form.docAdmiNote = { ...state.form.docAdmiNote, ...payload };
    },
    setFormNurseAdmiNote(state, payload) {
      state.form.nurseAdmiNote = { ...state.form.nurseAdmiNote, ...payload };
    },
    setFormPatAdmiNote(state, payload) {
      state.form.patAdmiNote = { ...state.form.patAdmiNote, ...payload };
    },
    setDictionaries(state, payload: IqueryDictionaries.IBusObj) {
      state.dictionaries = payload;
    },
    setHandleProvCityCounty(state, payload) {
      state.provCityCountyColumns = payload;
    },
    setRegister(
      state,
      payload: { registerProv: string; registerCity: string; registerCounty: string },
    ) {
      //户口 直接赋值没有code
      const { registerProv, registerCity, registerCounty } = payload;
      if (state.form.patAdmiNote) {
        state.form.patAdmiNote.registerProv = registerProv;
        state.form.patAdmiNote.registerCity = registerCity;
        state.form.patAdmiNote.registerCounty = registerCounty;
      }
    },
    setContactAddress(
      state,
      payload: {
        contactAddressProv: string;
        contactAddressCity: string;
        contactAddressCounty: string;
      },
    ) {
      //联系人住址 直接赋值没有code
      const { contactAddressProv, contactAddressCity, contactAddressCounty } = payload;
      if (state.form.patAdmiNote) {
        state.form.patAdmiNote.contactAddressProv = contactAddressProv;
        state.form.patAdmiNote.contactAddressCity = contactAddressCity;
        state.form.patAdmiNote.contactAddressCounty = contactAddressCounty;
      }
    },
    setBirthProvAndBirthCityAndBirthCounty(
      state,
      payload: {
        birthProv: { code: any; name: any };
        birthCity: { code: any; name: any };
        birthCounty: { code: any; name: any };
      },
    ) {
      const { birthProv, birthCity, birthCounty } = payload;
      if (state.form.patAdmiNote) {
        state.form.patAdmiNote.birthProv = birthProv;
        state.form.patAdmiNote.birthCity = birthCity;
        state.form.patAdmiNote.birthCounty = birthCounty;
      }
    },

    //设置当前选中的用户
    setCurrentUser(state, payload: IqueryBindPatInfo.IBusObj) {
      state.currentUser = payload;
    },
  },
  actions: {
    //处理户口地址省市区
    registerAction({ commit }, payload) {
      commit('setRegister', payload);
    },
    //处理出生地
    birthProvAndBirthCityAndBirthCountyAction({ commit }, payload) {
      commit('setBirthProvAndBirthCityAndBirthCounty', payload);
    },
    //处理联系人住址
    contactAddressAction({ commit }, payload) {
      commit('setContactAddress', payload);
    },
    IsConfirmAction({ commit }, payload) {
      commit('setIsConfirm', payload);
    },
    formAction({ commit }, payload) {
      commit('setForm', payload);
    },
    formItemAction({ commit }, payload: IpayloadItem) {
      commit('setFormItem', payload);
    },
    formDocAdmiNoteAction({ commit }, payload) {
      commit('setFormDocAdmiNote', payload);
    },
    formNurseAdmiNoteAction({ commit }, payload) {
      commit('setFormNurseAdmiNote', payload);
    },
    formPatAdmiNoteAction({ commit }, payload) {
      commit('setFormPatAdmiNote', payload);
    },
    dictionariesAction({ commit }, payload) {
      commit('setDictionaries', payload);
    },
    provCityCountyAction({ commit }, payload) {
      commit('setProvCityCounty', payload);
    },
    currentUserAction({ commit }, payload: IqueryBindPatInfo.IBusObj) {
      commit('setCurrentUser', payload);
    },

    //处理省市区字点为了里面的code
    handleProvCityCountyAction({ commit }, resData) {
      const provsData: any = [];
      const citysData = {};
      let allCitys: any = [];
      const distsData = {};

      resData.forEach((v: any) => {
        v.text = v.itemsName;
        v.value = v.itemsCode;
        if (v.upperCode === '0') {
          provsData.push(v);
        }
      });

      //省
      provsData.forEach((provs: any) => {
        const citys = resData.filter((v: any) => v.upperCode === provs.itemsCode);
        citys.forEach((i: any) => {
          i.children = [];
        });
        citysData[provs.itemsCode as string] = citys;
        allCitys = allCitys.concat(citys);
        provs.children = [];
      });

      allCitys.forEach((citys: any) => {
        const dists = resData.filter((v: any) => v.upperCode === citys.itemsCode);
        distsData[citys.itemsCode] = dists;
      });
      const areaData = {
        provsData,
        citysData,
        distsData,
      };

      const columns = [
        /*{
          text: '浙江',
          children: [
            {
              text: '杭州',
              children: [{ text: '西湖区' }, { text: '余杭区' }],
            },
            {
              text: '温州',
              children: [{ text: '鹿城区' }, { text: '瓯海区' }],
            },
          ],
        }*/
      ];
      for (const key in citysData) {
        if (citysData[key] && citysData[key].length) {
          citysData[key].forEach((i: any) => {
            for (const distsKey in distsData) {
              if (i.itemsCode == distsKey) {
                i.children = distsData[distsKey];
              }
            }
          });
        }
      }
      provsData.forEach((i: any) => {
        for (const key in citysData) {
          if (i.itemsCode == key) {
            i.children = citysData[key];
          }
        }
      });
      commit('setHandleProvCityCounty', provsData);
      // console.log('处理后省市区：', areaData);
    },
  },
  modules: {},
});
